<?php
/**************
* DESCRIPTION *
***************
* Generate an image with a keyword
*
* Usage :
* -------
* call create() method. See create() documentation
* for more details
*
*/
	class GoogleImage
	{
		private $defaultWords = array("chocolat", "concombre", "doigt", "log", "bopl", "nymac", "exoanima", "pilou", "durss", "air", "caca", "avion");
		private $directory = "assets/usersDatas/appearence/";

		/****************
		** CONSTRUCTOR **
		****************/
		public function __construct()	{	}



		/*******************
		** PUBLIC METHODS **
		*******************/

		/** Create an image
		* 
		* @param word {String}	:	Keyword for google request
		* @param type {String}	:	Get "large" or "icon" images
		* @param width {Number}	:	Width of the final image;
		* @param height {Number}	:	Height of the final image;
		* @param quality {Number}:	Image quality 0(bad)-100(good);
		* @return	 Generated image URL
		**/
		public function create($word, $type="icon", $imgW=250, $imgH=250, $quality=90)
		{
			//Tests offline
			//return "http://localhost/logout/dtc/img2D/Apparence/apparence5.jpg";

			if($type != "icon" && $type != "large")
				$type	= "icon";

			set_time_limit(0);
			$this->keyWord	= $word;
			$res			= $this->getContentPage($word);
			$array		= $this->getImage($res, $type);
			if(count($array) == 0){
				$res		= $this->getContentPage($this->defaultWords[rand(0, count($this->defaultWords)-1)]);
				$array	= $this->getImage($res, $type);
			}

			//Create image
			$image	= imagecreatetruecolor($imgW, $imgH);
			$images	= array();
			for($i=0; $i<count($array); $i++){
				$imgname	= $array[$i];
				$infos	= getimagesize($imgname);
				$ext		= strtolower(substr($infos['mime'], strrpos($infos['mime'], "/")+1));
				switch($ext){
					case "jpg":
						$src = @imagecreatefromjpeg($imgname);
						break;
					case "jpeg":
						$src = @imagecreatefromjpeg($imgname);
						break;
					case "gif":
						$src = @imagecreatefromgif($imgname);
						break;
					case "png":
						$src = @imagecreatefrompng($imgname);
						break;
				}
				if($src != false){
					$images[$i][0]	= $src;
					$images[$i][1]	= $infos;
				}
			}
			for($j=0; $j<3; $j++){
				shuffle($images);
				for($i=0; $i<count($images); $i++){
					$infos = $images[$i][1];
					imagecopy($image, $images[$i][0], rand(0, $imgW-$infos[0]) , rand(0, $imgH-$infos[1]), 0, 0, $infos[0], $infos[1]);
				}
			}

			$file = $this->directory.$_SESSION['user_id'].".jpg";
			imagejpeg($image, "../../../".$file, $quality);
			return $file;
		}



		/********************
		** PRIVATE METHODS **
		********************/

		/** Get content of a google image page **/
		private function getContentPage($word)
		{
			$this->datas	= "";
			$myFile		= "http://images.google.fr/images?hl=fr&q=".urlencode($word);
			$ch			= curl_init();
			curl_setopt	($ch, CURLOPT_URL, $myFile);
			curl_setopt	($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.2a) Gecko/20021021");
			curl_setopt	($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt	($ch, CURLOPT_FOLLOWLOCATION, 1);
			$datas		= curl_exec($ch);
			curl_close($ch);
			return $datas;
		}

		/** Get url images of google image page **/
		private function getImage($datas, $type)
		{
			$pattern	= "#dyn.Img\(\"([^\"]*)\" ?, ?\"([^\"]*)\" ?, ?\"([^\"]*)\" ?, ?\"([^\"]*)\" ?, ?\"([^\"]*)\" ?, ?\"([^\"]*)\" ?, ?\"([^\"]*)\" ?, ?\"([^\"]*)\" ?, ?\"([^\"]*)\" ?, ?\"([^\"]*)\" ?, ?\"([^\"]*)\" ?, ?\"([^\"]*)\" ?, ?\"([^\"]*)\" ?, ?\"([^\"]*)\" ?, ?\"([^\"]*)\" ?, ?\"([^\"]*)\"\);#is";
			preg_match_all($pattern, $datas, $matches, PREG_PATTERN_ORDER);
			$return	= array();

			if($type == "icon"){
				for($i=0; $i<count($matches[1]); $i++)
					$return[$i]	= urldecode($matches[15][$i]."?q=tbn:".$matches[3][$i]."".$matches[4][$i]);
			}else{
				for($i=0; $i<count($matches[1]); $i++)
					$return[$i]	= urldecode($matches[4][$i]);
			}
			return $return;
		}
	}

?>